package com.example.industry.service.impl;

import com.example.industry.dao.UserDao;
import com.example.industry.entity.Project;
import com.example.industry.entity.User;
import com.example.industry.enums.Roles;
import com.example.industry.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.util.List;

@Service
public class UserServiceImpl implements UserService
{
    @Resource
    private UserDao userDao;

    @Override
    public User findUserById(Integer userId)
    {
        return userDao.findById(userId);
    }

    @Override
    public User findUserByEmail(String email)
    {
        return userDao.findByEmail(email);
    }

    @Override
    public boolean register(String username, String email, String password)
    {
        User findUser = findUserByEmail(email);
        if (findUser == null)
        {
            User newUser = new User();
            newUser.setUsername(username);
            newUser.setEmail(email);
            newUser.setPassword(password);
            return userDao.addUser(newUser);
        }
        return false;
    }

    @Override
    public boolean login(String email, String password, HttpSession session)
    {
        User user = findUserByEmail(email);
        if (user != null && password.equals(user.getPassword()))
        {
            session.setAttribute("user", user);
            return true;
        }
        return false;
    }

    @Override
    public void logout(HttpSession session)
    {
        session.invalidate();
    }

    @Override
    public List<Project> findProjectsByUserIdAndRole(Integer userId, Roles role)
    {
        // TODO: 获取一个用户参与的Project, 指定角色
//        ResultSet resultSet = userDao.findProjectsByUserIdAndRole(userId, role);
//        if (resultSet == null)
//        {
//            return null;
//        }
//
//        while (resultSet.next())
//        {
//            resultSet.getString("userId");
//        }
        return null;
    }
}
